home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / ui / window.c < prev   
C/C++ Source or Header  |  1998-08-08  |  13KB  |  623 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17.  
  18. #include "mem.h"
  19. #include "fix.h"
  20. #include "types.h"
  21. #include "gr.h"
  22. #include "ui.h"
  23. #include "key.h"
  24.  
  25. #include "mono.h"
  26. #include "mouse.h"
  27. #include "timer.h"
  28.  
  29. #define _enable()
  30. #define _disable()
  31.  
  32. #define W_BACKGROUND    (wnd->background )
  33. #define W_X             (wnd->x)
  34. #define W_Y             (wnd->y)
  35. #define W_WIDTH         (wnd->width)
  36. #define W_HEIGHT        (wnd->height)
  37. #define W_OLDCANVAS     (wnd->oldcanvas)
  38. #define W_CANVAS        (wnd->canvas)
  39. #define W_GADGET        (wnd->gadget)
  40. #define W_TEXT_X        (wnd->text_x)
  41. #define W_TEXT_Y        (wnd->text_y)
  42. #define W_NEXT          (wnd->next)
  43. #define W_PREV          (wnd->prev)
  44.  
  45.  
  46. UI_WINDOW * CurWindow = NULL;
  47. UI_WINDOW * FirstWindow = NULL;
  48. UI_WINDOW * LastWindow = NULL;
  49.  
  50. int last_keypress = 0;
  51.  
  52. #define BORDER_WIDTH 8
  53.  
  54. static unsigned int FrameCount = 0;
  55. unsigned int ui_event_counter = 0;
  56. unsigned int ui_number_of_events = 0;
  57. static UI_EVENT *   EventBuffer = NULL;
  58. static int          Record = 0;
  59. static int          RecordFlags = 0;
  60.  
  61. static short MouseDX=0, MouseDY=0, MouseButtons=0;
  62.  
  63. static unsigned char SavedState[256];
  64.  
  65. static int PlaybackSpeed = 1;
  66.  
  67. extern void ui_draw_frame( short x1, short y1, short x2, short y2 );
  68.  
  69. // 1=1x faster, 2=2x faster, etc
  70. void ui_set_playback_speed( int speed )
  71. {
  72.     PlaybackSpeed = speed;
  73. }
  74.  
  75. int ui_record_events( int NumberOfEvents, UI_EVENT * buffer, int Flags )
  76. {
  77.     if ( Record > 0 || buffer==NULL ) return 1;
  78.  
  79.     RecordFlags = Flags;
  80.     EventBuffer = buffer;
  81.     ui_event_counter = 0;
  82.     FrameCount = 0;
  83.     ui_number_of_events = NumberOfEvents;
  84.     Record = 1;
  85.     return 0;
  86. }
  87.  
  88. int ui_play_events_realtime( int NumberOfEvents, UI_EVENT * buffer )
  89. {    int i;
  90.     if ( buffer == NULL ) return 1;
  91.  
  92.     EventBuffer = buffer;
  93.     FrameCount = 0;
  94.     ui_event_counter = 0;
  95.     ui_number_of_events = NumberOfEvents;
  96.     Record = 2;
  97.     _disable();
  98.     keyd_last_released= 0;
  99.     keyd_last_pressed= 0;
  100.     for (i=0; i<256; i++ )
  101.         SavedState[i] = keyd_pressed[i];
  102.     _enable();
  103.     key_flush();
  104.     return 0;
  105. }
  106.  
  107. int ui_play_events_fast( int NumberOfEvents, UI_EVENT * buffer )
  108. {
  109.     int i;
  110.     if ( buffer == NULL ) return 1;
  111.  
  112.     EventBuffer = buffer;
  113.     FrameCount = 0;
  114.     ui_event_counter = 0;
  115.     ui_number_of_events = NumberOfEvents;
  116.     Record = 3;
  117.     _disable();
  118.     keyd_last_released= 0;
  119.     keyd_last_pressed= 0;
  120.  
  121.     //mprintf( 0, "Before: ", i );
  122.     for (i=0; i<256; i++ )
  123.     {
  124.         //if (keyd_pressed[i]) mprintf( 0, "%d ", i );
  125.         SavedState[i] = keyd_pressed[i];
  126.     }
  127.     //mprintf( 0, "\n" );
  128.     _enable();
  129.     key_flush();
  130.     return 0;
  131. }
  132.  
  133. // Returns:  0=Normal, 1=Recording, 2=Playback normal, 3=Playback fast
  134. int ui_recorder_status()
  135. {
  136.     return Record;
  137. }
  138.  
  139. void add_window_to_end( UI_WINDOW * wnd )
  140. {
  141.     if (LastWindow) {
  142.         W_PREV = LastWindow;
  143.         LastWindow->next = wnd;
  144.     }
  145.     LastWindow = wnd;
  146.     if (!FirstWindow)
  147.         FirstWindow = wnd;
  148. }
  149.  
  150. void add_window_to_beg( UI_WINDOW * wnd )
  151. {
  152.     if (FirstWindow) {
  153.         W_NEXT = FirstWindow;
  154.         FirstWindow->prev = wnd;
  155.     }
  156.     FirstWindow = wnd;
  157.     if (!LastWindow)
  158.         LastWindow = wnd;
  159. }
  160.  
  161. // Add w1 after w2
  162. void add_window_after( UI_WINDOW * w1, UI_WINDOW * w2 )
  163. {
  164.     w1->prev = w2;
  165.     w1->next = w2->next;
  166.     w2->next = w1;
  167.     if (w1->next == NULL )
  168.         LastWindow = w1;
  169.     else
  170.         w1->next->prev = w1;
  171. }
  172.  
  173. void close_all()
  174. {
  175.     UI_WINDOW *sav, *wnd = LastWindow;
  176.  
  177.     while(wnd)
  178.     {
  179.         sav = W_PREV;
  180.         ui_close_window(wnd);
  181.         wnd = sav;
  182.     }
  183. }
  184.  
  185. void remove_window( UI_WINDOW * wnd )
  186. {
  187.     if (W_NEXT)
  188.         W_NEXT->prev = W_PREV;
  189.     if (W_PREV)
  190.         W_PREV->next = W_NEXT;
  191.     if (FirstWindow == wnd )
  192.         FirstWindow = W_NEXT;
  193.     if (LastWindow == wnd )
  194.         LastWindow = W_PREV;
  195.     W_NEXT = W_PREV = NULL;
  196. }
  197.  
  198.  
  199.  
  200. UI_WINDOW * ui_open_window( short x, short y, short w, short h, int flags )
  201. {
  202.     UI_WINDOW * wnd;
  203.     int sw, sh, req_w, req_h;
  204.  
  205.     wnd = (UI_WINDOW *)malloc(sizeof(UI_WINDOW));
  206.     if (wnd==NULL) exit(1);
  207.  
  208.     W_NEXT = NULL;
  209.     W_PREV = NULL;
  210.  
  211.     add_window_to_end( wnd );
  212.  
  213.     sw = grd_curscreen->sc_w;
  214.     sh = grd_curscreen->sc_h;
  215.  
  216.     mouse_set_limits( 0,0, sw-1, sh-1 );
  217.  
  218.     req_w = w;
  219.     req_h = h;
  220.  
  221.     if (flags & WIN_BORDER)
  222.     {
  223.         x -= BORDER_WIDTH;
  224.         y -= BORDER_WIDTH;
  225.         w += 2*BORDER_WIDTH;
  226.         h += 2*BORDER_WIDTH;
  227.     }
  228.  
  229.     if ( x < 0 ) x = 0;
  230.     if ( (x+w-1) >= sw ) x = sw - w;
  231.     if ( y < 0 ) y = 0;
  232.     if ( (y+h-1) >= sh ) y = sh - h;
  233.  
  234.     W_X = x;
  235.     W_Y = y;
  236.     W_WIDTH = w;
  237.     W_HEIGHT = h;
  238.     W_OLDCANVAS = grd_curcanv;
  239.     W_GADGET = NULL;
  240.     wnd->keyboard_focus_gadget = NULL;
  241.  
  242.     ui_mouse_hide();
  243.  
  244.     if (flags & WIN_SAVE_BG)
  245.     {
  246.         W_BACKGROUND = gr_create_bitmap( w, h );
  247.         gr_bm_ubitblt(w, h, 0, 0, x, y, &(grd_curscreen->sc_canvas.cv_bitmap), W_BACKGROUND );
  248.     }
  249.     else
  250.         W_BACKGROUND = NULL;
  251.  
  252.     if (flags & WIN_BORDER)
  253.     {
  254.         W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x+BORDER_WIDTH, y+BORDER_WIDTH, req_w, req_h );
  255.         gr_set_current_canvas( NULL );
  256.         ui_draw_frame( x, y, x+w-1, y+h-1 );
  257.     }
  258.     else
  259.         W_CANVAS = gr_create_sub_canvas( &(grd_curscreen->sc_canvas), x, y, req_w, req_h );
  260.  
  261.     gr_set_current_canvas( W_CANVAS );
  262.  
  263.     if (flags & WIN_FILLED)
  264.         ui_draw_box_out( 0, 0, req_w-1, req_h-1 );
  265.  
  266.     gr_set_fontcolor( CBLACK, CWHITE );
  267.  
  268.     selected_gadget = NULL;
  269.  
  270.     W_TEXT_X = 0;
  271.     W_TEXT_Y = 0;
  272.  
  273.     return wnd;
  274.  
  275. }
  276.  
  277. void ui_close_window( UI_WINDOW * wnd )
  278. {
  279.  
  280.     ui_mouse_hide();
  281.  
  282.     ui_gadget_delete_all( wnd );
  283.  
  284.     if (W_BACKGROUND)
  285.     {
  286.         gr_bm_ubitblt(W_WIDTH, W_HEIGHT, W_X, W_Y, 0, 0, W_BACKGROUND, &(grd_curscreen->sc_canvas.cv_bitmap));
  287.         gr_free_bitmap( W_BACKGROUND );
  288.     } else
  289.     {
  290.         gr_set_current_canvas( NULL );
  291.         gr_setcolor( CBLACK );
  292.         gr_rect( W_X, W_Y, W_X+W_WIDTH-1, W_Y+W_HEIGHT-1 );
  293.     }
  294.  
  295.     gr_free_sub_canvas( W_CANVAS );
  296.  
  297.     gr_set_current_canvas( W_OLDCANVAS );
  298.  
  299.     selected_gadget = NULL;
  300.  
  301.     remove_window( wnd );
  302.  
  303.     if (CurWindow==wnd)
  304.         CurWindow = NULL;
  305.  
  306.     free( wnd );
  307.  
  308.     ui_mouse_show();
  309. }
  310.  
  311. restore_state()
  312. {
  313.     int i;
  314.     _disable();
  315.     //mprintf( 0, "After: " );
  316.     for (i=0; i<256; i++ )
  317.     {
  318.         //if (SavedState[i]) mprintf( 0, "%d ", i );
  319.         keyd_pressed[i] = SavedState[i];
  320.     }
  321.     //mprintf( 0, "\n" );
  322.     _enable();
  323. }
  324.  
  325.  
  326. int last_event = 0;
  327.  
  328. void ui_reset_idle_seconds()
  329. {
  330.     last_event = TICKER;
  331. }
  332.  
  333. int ui_get_idle_seconds()
  334. {
  335.     return (((TICKER - last_event)*10)/182);
  336. }
  337.  
  338. void ui_mega_process()
  339. {
  340.     int mx, my;
  341.     unsigned char k;
  342.     
  343.     switch( Record )
  344.     {
  345.     case 0:
  346.         mouse_get_delta( &mx, &my );
  347.         Mouse.new_dx = mx;
  348.         Mouse.new_dy = my;
  349.         Mouse.new_buttons = mouse_get_btns();
  350.         last_keypress = key_inkey();
  351.  
  352.         if ( Mouse.new_buttons || last_keypress || Mouse.new_dx || Mouse.new_dy )    {
  353.             last_event = TICKER;
  354.         }
  355.  
  356.         break;
  357.     case 1:
  358.  
  359.         if (ui_event_counter==0 )
  360.         {
  361.             EventBuffer[ui_event_counter].frame = 0;
  362.             EventBuffer[ui_event_counter].type = 7;
  363.             EventBuffer[ui_event_counter].data = ui_number_of_events;
  364.             ui_event_counter++;
  365.         }
  366.  
  367.  
  368.         if (ui_event_counter==1 && (RecordFlags & UI_RECORD_MOUSE) )
  369.         {
  370.             Mouse.new_buttons = 0;
  371.             EventBuffer[ui_event_counter].frame = FrameCount;
  372.             EventBuffer[ui_event_counter].type = 6;
  373.             EventBuffer[ui_event_counter].data = ((Mouse.y & 0xFFFF) << 16) | (Mouse.x & 0xFFFF);
  374.             ui_event_counter++;
  375.         }
  376.  
  377.         mouse_get_delta( &mx, &my );
  378.         MouseDX = mx;
  379.         MouseDY = my;
  380.         MouseButtons = mouse_get_btns();
  381.  
  382.         Mouse.new_dx = MouseDX;
  383.         Mouse.new_dy = MouseDY;
  384.  
  385.         if ((MouseDX != 0 || MouseDY != 0)  && (RecordFlags & UI_RECORD_MOUSE)  )
  386.         {
  387.             if (ui_event_counter < ui_number_of_events-1 )
  388.             {
  389.                 EventBuffer[ui_event_counter].frame = FrameCount;
  390.                 EventBuffer[ui_event_counter].type = 1;
  391.                 EventBuffer[ui_event_counter].data = ((MouseDY & 0xFFFF) << 16) | (MouseDX & 0xFFFF);
  392.                 ui_event_counter++;
  393.                 //mprintf( 0, "EVENT:%d, Mouse moved %d,%d\n", ui_event_counter, MouseDX, MouseDY );
  394.             } else {
  395.                 Record = 0;
  396.             }
  397.  
  398.         }
  399.  
  400.         if ( (MouseButtons != Mouse.new_buttons) && (RecordFlags & UI_RECORD_MOUSE) )
  401.         {
  402.             Mouse.new_buttons = MouseButtons;
  403.  
  404.             if (ui_event_counter < ui_number_of_events-1 )
  405.             {
  406.                 EventBuffer[ui_event_counter].frame = FrameCount;
  407.                 EventBuffer[ui_event_counter].type = 2;
  408.                 EventBuffer[ui_event_counter].data = MouseButtons;
  409.                 ui_event_counter++;
  410.                 //mprintf( 0, "EVENT:%d, Mouse buttons changed %d\n", ui_event_counter, MouseButtons );
  411.             } else {
  412.                 Record = 0;
  413.             }
  414.  
  415.         }
  416.  
  417.  
  418.         if ( keyd_last_pressed && (RecordFlags & UI_RECORD_KEYS) )
  419.         {
  420.             _disable();
  421.             k = keyd_last_pressed;
  422.             keyd_last_pressed= 0;
  423.             _enable();
  424.  
  425.             if (ui_event_counter < ui_number_of_events-1 )
  426.             {
  427.                 EventBuffer[ui_event_counter].frame = FrameCount;
  428.                 EventBuffer[ui_event_counter].type = 3;
  429.                 EventBuffer[ui_event_counter].data = k;
  430.                 ui_event_counter++;
  431.                 //mprintf( 0, "EVENT:%d, Key %d pressed\n", ui_event_counter, k );
  432.             } else {
  433.                 Record = 0;
  434.             }
  435.  
  436.         }
  437.  
  438.         if ( keyd_last_released && (RecordFlags & UI_RECORD_KEYS) )
  439.         {
  440.             _disable();
  441.             k = keyd_last_released;
  442.             keyd_last_released= 0;
  443.             _enable();
  444.  
  445.             if (ui_event_counter < ui_number_of_events-1 )
  446.             {
  447.                 EventBuffer[ui_event_counter].frame = FrameCount;
  448.                 EventBuffer[ui_event_counter].type = 4;
  449.                 EventBuffer[ui_event_counter].data = k;
  450.                 ui_event_counter++;
  451.                 //mprintf( 0, "EVENT:%d, Key %d released\n", ui_event_counter, k );
  452.             } else {
  453.                 Record = 0;
  454.             }
  455.         }
  456.  
  457.         last_keypress = key_inkey();
  458.  
  459.         if (last_keypress == KEY_F12 )
  460.         {
  461.             ui_number_of_events = ui_event_counter;
  462.             last_keypress = 0;
  463.             Record = 0;
  464.             break;
  465.         }
  466.  
  467.         if ((last_keypress != 0) && (RecordFlags & UI_RECORD_KEYS) )
  468.         {
  469.             if (ui_event_counter < ui_number_of_events-1 )
  470.             {
  471.                 EventBuffer[ui_event_counter].frame = FrameCount;
  472.                 EventBuffer[ui_event_counter].type = 5;
  473.                 EventBuffer[ui_event_counter].data = last_keypress;
  474.                 ui_event_counter++;
  475.                 //mprintf( 0, "EVENT:%d, Keypressed %d\n", ui_event_counter, last_keypress );
  476.             } else {
  477.                 Record = 0;
  478.             }
  479.         }
  480.  
  481.         FrameCount++;
  482.  
  483.         break;
  484.     case 2:
  485.     case 3:
  486.         Mouse.new_dx = 0;
  487.         Mouse.new_dy = 0;
  488.         Mouse.new_buttons = 0;
  489.         last_keypress = 0;
  490.  
  491.         if ( keyd_last_pressed ) {
  492.             _disable();            
  493.             k = keyd_last_pressed;
  494.             keyd_last_pressed = 0;
  495.             _disable();
  496.             SavedState[k] = 1;
  497.         }
  498.  
  499.         if ( keyd_last_released ) 
  500.         {
  501.             _disable();            
  502.             k = keyd_last_released;
  503.             keyd_last_released = 0;
  504.             _disable();
  505.             SavedState[k] = 0;
  506.         }
  507.         
  508.         if (key_inkey() == KEY_F12 )
  509.         {
  510.             //mprintf( 0, "Playing stopped.\n" );
  511.             restore_state();
  512.             Record = 0;
  513.             break;
  514.         }
  515.  
  516.         if (EventBuffer==NULL) {
  517.             restore_state();
  518.             Record = 0;
  519.             break;
  520.         }
  521.  
  522.         while( (ui_event_counter < ui_number_of_events) && (EventBuffer[ui_event_counter].frame <= FrameCount) )
  523.         {
  524.             switch ( EventBuffer[ui_event_counter].type )
  525.             {
  526.             case 1:     // Mouse moved
  527.                 Mouse.new_dx = EventBuffer[ui_event_counter].data & 0xFFFF;
  528.                 Mouse.new_dy = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
  529.                 break;
  530.             case 2:     // Mouse buttons changed
  531.                 Mouse.new_buttons = EventBuffer[ui_event_counter].data;
  532.                 break;
  533.             case 3:     // Key moved down
  534.                 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 1;
  535.                 break;
  536.             case 4:     // Key moved up
  537.                 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 0;
  538.                 break;
  539.             case 5:     // Key pressed
  540.                 last_keypress = EventBuffer[ui_event_counter].data;
  541.                 break;
  542.             case 6:     // Initial Mouse X position
  543.                 Mouse.x = EventBuffer[ui_event_counter].data & 0xFFFF;
  544.                 Mouse.y = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
  545.                 break;
  546.             case 7:
  547.                 break;
  548.             }
  549.             ui_event_counter++;
  550.             if (ui_event_counter >= ui_number_of_events )
  551.             {
  552.                 Record = 0;
  553.                 restore_state();
  554.                 //( 0, "Done playing %d events.\n", ui_number_of_events );
  555.             }
  556.         }
  557.  
  558.         switch (Record)
  559.         {
  560.         case 2:
  561.             {
  562.                 int next_frame;
  563.             
  564.                 if ( ui_event_counter < ui_number_of_events )
  565.                 {
  566.                     next_frame = EventBuffer[ui_event_counter].frame;
  567.                     
  568.                     if ( (FrameCount+PlaybackSpeed) < next_frame )
  569.                         FrameCount = next_frame - PlaybackSpeed;
  570.                     else
  571.                         FrameCount++;
  572.                 } else {
  573.                      FrameCount++;
  574.                 }    
  575.             }
  576.             break;
  577.  
  578.         case 3:            
  579.              if ( ui_event_counter < ui_number_of_events ) 
  580.                 FrameCount = EventBuffer[ui_event_counter].frame;
  581.             else         
  582.                 FrameCount++;
  583.             break;
  584.         default:        
  585.             FrameCount++;
  586.         }
  587.     }
  588.  
  589.     ui_mouse_process();
  590.  
  591. }
  592.  
  593. void ui_wprintf( UI_WINDOW * wnd, char * format, ... )
  594. {
  595.     char buffer[1000];
  596.     va_list args;
  597.  
  598.     va_start(args, format );
  599.     vsprintf(buffer,format,args);
  600.  
  601.     gr_set_current_canvas( W_CANVAS );
  602.  
  603.     ui_mouse_hide();
  604.     W_TEXT_X = gr_string( W_TEXT_X, W_TEXT_Y, buffer );
  605.     ui_mouse_show();
  606. }
  607.  
  608. void ui_wprintf_at( UI_WINDOW * wnd, short x, short y, char * format, ... )
  609. {
  610.     char buffer[1000];
  611.     va_list args;
  612.  
  613.     va_start(args, format );
  614.     vsprintf(buffer,format,args);
  615.  
  616.     gr_set_current_canvas( W_CANVAS );
  617.  
  618.     ui_mouse_hide();
  619.     gr_string( x, y, buffer );
  620.     ui_mouse_show();
  621.  
  622. }
  623.